/**
 * 
 *
 * @author fan.ch
 * @date 2017-07-01
 * @copyright jianjia.com
 *
 */
'use strict';
var express = require('express');
var router = express.Router();
var request = require('request');
var mongoose = require('mongoose');
var url = require("url");
var path = require('path');
var db = require(path.join(__dirname, "../../tools/basicServer.js"));
var app = express();
var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
	extended: false
}));

router.all('/isValid', function(req, res, next) {
	var params = req.body;
	db.findOne("log", params, function(docs) {
		let doc = docs || {};
		res.setHeader("Content-Type", "text/html;charset=utf-8");
		if(doc.name === params.name && doc.password === params.password) {
			//登录成功，根据角色获取顶部的导航
			db.findOne("showMenu", {
				"role": doc.role
			}, function(menu) {
				res.send({
					menuData:menu.menuData,
					loginName: params.name,
					result: 1
				})
			})
		} else {
			res.send({
				result: -1
			})
		}
	})
});

router.all('/addRole', function(req, res, next) {
	var params = req.body;
	params.date = db.dealDate();
	db.findOr("log", [{
		'companyName': params.companyName
	}, {
		'name': params.name
	}], function(docs) {
		let doc = docs || {};
		res.setHeader("Content-Type", "json/html;charset=utf-8");
		if(doc.length > 0) {
			doc.forEach(function(value, index, arr) {
				if(value.companyName === params.companyName) {
					res.send({
						msg: '此公司已经创建账号',
						data: {
							'name': params.name
						},
						result: -1
					})
					return;
				} else if(value.name === params.name) {
					res.send({
						msg: '账号重复',
						data: {
							'name': params.name
						},
						result: -2
					})
					return;
				}

			});

		} else {
			db.insertOne("log", params, function() {
				res.send({
					msg: '创建成功',
					result: 1
				})
			})

		}

	})

});
//请求侧边栏
router.all('/getLeftMenu', function(req, res, next) {
	var menuType = req.body; //通过传递顶部导航类型，查询侧边栏的数据
	db.findOne('leftMenu', menuType, function(docs) {
		let doc = docs || {};
		if(doc) {
			res.send({
				msg: "查询成功",
				leftInfo:doc.leftInfo,
				result: 1
			})
		}
	})
})
//查询账号表格
router.all('/companyInfo', function(req, res, next) {
	var size = req.body.size; //每页展示条数
	var currentPage = req.body.currentPage; //当前页码
	db.findAll('log', req.body, function(docs) {
		let doc = docs || {};
		if(doc) {
			docs = docs.slice((currentPage - 1) * size, ((currentPage - 1) * size + size))
			res.send({
				msg: "查询成功",
				data: {
					total: doc.length,
					row: docs
				},
				result: 1
			})
		}
	})
})
//导出账号数据表格
router.all('/exportMembers', function(req, res, next) {
	var _headers=[{
		"caption":"公司名称",
		"type":"string"
	},{
		"caption":"公司地址",
		"type":"string"
	},{
		"caption":"公司联系电话",
		"type":"string"
	},{
		"caption":"登陆名",
		"type":"string"
	},{
		"caption":"登陆角色",
		"type":"string"
	},{
		"caption":"创建时间",
		"type":"string"
	}]
	
	db.findAll('log', req.body, function(docs) {
//		console.log(docs)
		let doc = docs || {};
		var result=db.exportExcel(_headers,doc,'公司信息明细')
		res.setHeader('Content-Type', 'application/vnd.openxmlformats');
        res.setHeader("Content-Disposition", "attachment; filename=" + "test.xlsx");
        console.log(result)
        res.send(result, 'binary');
        
        return;
	})
})


module.exports = router;